package com.training.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.base.BaseActivity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.training.adapter.CourseListAdapter;
import com.training.bean.Course;
import com.training.databinding.ActivityCourseListBinding;
import com.training.inter.OnCourseListItemClickListener;
import com.training.net.Constant;
import com.training.net.HttpResponse;

import java.util.ArrayList;

import com.training.net.NetCallback;
import com.training.util.ToastyUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CourseListActivity extends BaseActivity<ActivityCourseListBinding> {
    private int stageId;
    private CourseListAdapter adapter;

    @Override
    public ActivityCourseListBinding binding() {
        return ActivityCourseListBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onCreated(Bundle savedInstanceState) {
        Intent intent = getIntent();
        stageId = intent.getIntExtra("stageId",-1);
        mBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new CourseListAdapter(this, new OnCourseListItemClickListener() {
            @Override
            public void onItemClick(int position, Course data) {
                Intent intent = new Intent(context, CourseContentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("course", data);
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onCollectClick(int position, Course data) {
                dc.collect(data.id, new NetCallback<HttpResponse<String>>() {

                    @Override
                    public void onError(int code, String message) {
                        ToastyUtil.showError(context, message, Toast.LENGTH_SHORT, true);
                    }

                    @Override
                    public void onSuccess(HttpResponse<String> response) {
                        ToastyUtil.showSuccess(context, response.message, Toast.LENGTH_SHORT, true);
                    }

                    @Override
                    public void onFinally() {

                    }

                });
            }
        });
        mBinding.dataRv.setLayoutManager(new LinearLayoutManager(context));
        mBinding.dataRv.setAdapter(adapter);
        mBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
        mBinding.refreshLayout.autoRefresh();
    }

    @Override
    public void loadData() {
        dc.getAllCourse(stageId,new NetCallback<HttpResponse<ArrayList<Course>>>() {
            @Override
            public void onError(int code, String message) {
                ToastyUtil.showError(context, message, Toast.LENGTH_SHORT, true);
                finish();
            }

            @Override
            public void onSuccess(HttpResponse<ArrayList<Course>> response) {
                adapter.setData(response.data);
            }

            @Override
            public void onFinally() {
                finishRefresh();
            }
        });
    }

    @Override
    public void refreshUI() {

    }

    private void finishRefresh() {
        mBinding.refreshLayout.finishRefresh(200);
    }
}
